package com.lordjoe.collectiveintelligence;

import java.util.*;

/**
 * com.lordjoe.collectiveintelligence.BiCluster
 *
 * @author Steve Lewis
 * @date Feb 24, 2009
 */
public class BiCluster extends MultiCluster 
{
    public static BiCluster[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = BiCluster.class;

    private double m_Distance;
    private final int m_Weight;

    public BiCluster(ICluster pPart1, ICluster pPart2)
    {
        super(pPart1.getName() + "+" + pPart2.getName(),pPart1, pPart2) ;
        m_Weight = pPart1.getWeight() +  pPart2.getWeight();
    }

     public int getWeight()
    {
        return m_Weight;
    }

    protected double[]  buildValues()
    {
        ICluster p1 = getPart1();
        double[] val1 = p1.getValues();
        ICluster p2 = getPart2();
        double[] val2 = p2.getValues();
        int weight1 = p1.getWeight();
        int weight2 = p2.getWeight();
        double totalWeight = weight1 + weight2;
        double[] ret = new double[val1.length];
        for (int i = 0; i < ret.length; i++) {
             ret[i] = (weight1 * val1[i] +  weight2 * val2[i]) /totalWeight;
        }
        return ret;
    }

    public ICluster getPart1()
    {
        return getParts()[0];
    }


    public ICluster getPart2()
    {
        return getParts()[1];
    }

    public void addPart()
    {
        if (true) throw new UnsupportedOperationException("Fix This");

    }

    @Override
      public String toString()
      {
          return getName().substring(0,10) + " w=" +  getWeight();
      }


    public double getDistance()
    {
        return m_Distance;
    }
}
